﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace DALL.Order
{
    public class T_PUB_ORDER_CONTANCT
    {
        public T_PUB_ORDER_CONTANCT()
        { }

        public bool Add(Model.Order.T_PUB_ORDER_CONTANCT model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_PUB_ORDER_CONTANCT(");
            strSql.Append("Order_ID,Contents,Other,DateTime,UserID,Customer)");

            strSql.Append(" values (");
            strSql.Append("@Order_ID,@Contents,@Other,@DateTime,@UserID,@Customer)");
            strSql.Append(";select @@IDENTITY");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "Order_ID", DbType.String, model.Order_ID);
            db.AddInParameter(dbCommand, "Contents", DbType.String, model.Contents);
            db.AddInParameter(dbCommand, "UserID", DbType.Int32, model.UserID);
            db.AddInParameter(dbCommand, "Other", DbType.String, model.Other);
            db.AddInParameter(dbCommand, "Customer", DbType.String, model.Customer);
            db.AddInParameter(dbCommand, "DateTime", DbType.Time, model.DateTime);
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Order.T_PUB_ORDER_CONTANCT GetModelByOrderID(string id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 id,Order_ID,Contents,Other,DateTime,UserID,Customer from T_PUB_ORDER_CONTANCT ");
            strSql.Append(" where Order_ID=@Order_ID order by DateTime desc");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "Order_ID", DbType.String, id);
            Model.Order.T_PUB_ORDER_CONTANCT model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Contents,Other,DateTime,USER_NAME");
            strSql.Append(" FROM T_PUB_ORDER_CONTANCT,T_PUB_USER ");

            strSql.Append(" where T_PUB_ORDER_CONTANCT.UserID=T_PUB_USER.USER_ID");
            if (strWhere != "")
            {
                strSql.Append(strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public Model.Order.T_PUB_ORDER_CONTANCT ReaderBind(IDataReader dataReader)
        {
            Model.Order.T_PUB_ORDER_CONTANCT model = new Model.Order.T_PUB_ORDER_CONTANCT();
            object ojb;
            ojb = dataReader["id"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.id = (int)ojb;
            }
            model.Order_ID = dataReader["Order_ID"].ToString();
            model.Contents = dataReader["Contents"].ToString();
            model.Other = dataReader["Other"].ToString();
            ojb = dataReader["DateTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DateTime = (DateTime)ojb;
            }
            ojb = dataReader["UserID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.UserID = (int)ojb;
            }
            model.Customer = dataReader["Customer"].ToString();          
            return model;
        }

    }
}

